package com.example.yangyinzhaixian.AllActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yangyinzhaixian.Bean.Course2Bean;
import com.example.yangyinzhaixian.Customview.MLImageView;
import com.example.yangyinzhaixian.Fragemt.Course2.BlankFragment1;
import com.example.yangyinzhaixian.Fragemt.Course2.BlankFragment2;
import com.example.yangyinzhaixian.Fragemt.Course2.BlankFragment3;
import com.example.yangyinzhaixian.R;
import com.example.yangyinzhaixian.uitl.SharedPreferencesUtils;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import java.util.ArrayList;

import okhttp3.Call;

public class Course2Activity extends AppCompatActivity implements View.OnClickListener {


    private Button login_blank_btn;
    private TextView shezhi_text;
    private ImageView course2_image;
    private Button course2_btn_try_see;
    private MLImageView hand_image;
    private TextView teacher_name;
    private TextView teacher_title;
    private LinearLayout course2_linear1;
    private TextView course2_roomTitle;
    private TextView course2_price;
    private RelativeLayout course2_relative;
    private TabLayout course2_FindFragment_title;
    private ViewPager course2_viewpager;
    private LinearLayout activity_course2;
    private ArrayList<String> list_title;
    private ArrayList<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_course2);
        initView();
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String token = (String) SharedPreferencesUtils.getParam(this, "Token", "");
        OkHttpUtils.get().url("http://api.immedc.com/restapi/course/getVideo2")
                .addHeader("CH-TOKEN", token)
                .addParams("courseId",id)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(Course2Activity.this, e + "", Toast.LENGTH_SHORT).show();
                Log.d("TAG", "onError: " + e.getMessage());
                e.printStackTrace();
            }

            @Override
            public void onResponse(String response, int id) throws JSONException {
                Gson gson = new Gson();
                Course2Bean course2Bean = gson.fromJson(response, Course2Bean.class);
                //标题
                Course2Bean.DataBean data = course2Bean.getData();
                shezhi_text.setText(data.getCourseName());
                //图片
                Picasso.with(Course2Activity.this).load(data.getImage()).into(course2_image);
                //头像
                Picasso.with(Course2Activity.this).load(data.getTeacherAvatar()).into(hand_image);
                //信息
                teacher_name.setText(data.getTeacherName());
                teacher_title.setText(data.getTeacherTitle());
                course2_roomTitle.setText(data.getShareView().getShareTitle());
                //价格course2_price
                if (data.getChapterPrice()==0)
                {
                    course2_price.setText("免费");
                }
                else{
                    course2_price.setText(data.getChapterPrice()+"元");
                }
                //为TabLayout添加tab名称
                list_title = new ArrayList<>();
                list_title.add("本课知识");
                list_title.add("课时列表");
                list_title.add("作业要求");
                course2_FindFragment_title.addTab(course2_FindFragment_title.newTab().setText(list_title.get(0)));
                course2_FindFragment_title.addTab(course2_FindFragment_title.newTab().setText(list_title.get(1)));
                course2_FindFragment_title.addTab(course2_FindFragment_title.newTab().setText(list_title.get(2)));
                fragments = new ArrayList<>();
                fragments.add(new BlankFragment1(data));
                fragments.add(new BlankFragment2(data));
                fragments.add(new BlankFragment3(data));
                Course2Activity.Find_tab_Adapter find_tab_adapter = new Course2Activity.Find_tab_Adapter(getSupportFragmentManager(), fragments, list_title);
                course2_viewpager.setAdapter(find_tab_adapter);
                course2_FindFragment_title.setupWithViewPager(course2_viewpager);
            }
        });
    }
    class Find_tab_Adapter extends FragmentPagerAdapter {
        private ArrayList<String> list_title;
        private ArrayList<Fragment> fragments;

        public Find_tab_Adapter(FragmentManager fm, ArrayList<Fragment> fragments, ArrayList<String> list_title) {
            super(fm);
            this.fragments = fragments;
            this.list_title = list_title;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return list_title.size();
        }

        //此方法用来显示tab上的名字
        @Override
        public CharSequence getPageTitle(int position) {
            return list_title.get(position % list_title.size());
        }
    }
    private void initView() {

        login_blank_btn = (Button) findViewById(R.id.login_blank_btn);
        login_blank_btn.setOnClickListener(this);
        shezhi_text = (TextView) findViewById(R.id.shezhi_text);
        shezhi_text.setOnClickListener(this);
        course2_image = (ImageView) findViewById(R.id.course2_image);
        course2_image.setOnClickListener(this);
        course2_btn_try_see = (Button) findViewById(R.id.course2_btn_try_see);
        course2_btn_try_see.setOnClickListener(this);
        hand_image = (MLImageView) findViewById(R.id.hand_image);
        hand_image.setOnClickListener(this);
        teacher_name = (TextView) findViewById(R.id.teacher_name);
        teacher_name.setOnClickListener(this);
        teacher_title = (TextView) findViewById(R.id.teacher_title);
        teacher_title.setOnClickListener(this);
        course2_linear1 = (LinearLayout) findViewById(R.id.course2_linear1);
        course2_linear1.setOnClickListener(this);
        course2_roomTitle = (TextView) findViewById(R.id.course2_roomTitle);
        course2_roomTitle.setOnClickListener(this);
        course2_price = (TextView) findViewById(R.id.course2_price);
        course2_price.setOnClickListener(this);
        course2_relative = (RelativeLayout) findViewById(R.id.course2_relative);
        course2_relative.setOnClickListener(this);
        course2_FindFragment_title = (TabLayout) findViewById(R.id.course2_FindFragment_title);
        course2_FindFragment_title.setOnClickListener(this);
        course2_viewpager = (ViewPager) findViewById(R.id.course2_viewpager);
        course2_viewpager.setOnClickListener(this);
        activity_course2 = (LinearLayout) findViewById(R.id.activity_course2);
        activity_course2.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_blank_btn:

                break;
            case R.id.course2_btn_try_see:

                break;
        }
    }
}
